package com.sphd.emanu.fragment.aterials;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.JsonUtil;
import com.wzutils.L;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author Hu
 * @Time 2017/5/26 -- 14:57.
 * @Email 87683202@qq.com
 */

public class AterialsPresenter extends BasePresenterImpl<AterialsContract.View> implements AterialsContract.Presenter{

    @Override
    public void selectFristCategory(int org) {
        Call<String> call=emanuService.selectFristCategory(org);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                List<Category> tMtCategoryList= GsonUtil.jsonToList(response.body().toString(),Category.class);
                mView.setList(tMtCategoryList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void deleteMaterialCategory(Integer categoryId, final int pos, final SweetAlertDialog sweetAlertDialog) {
        emanuService.deleteMaterialCategory(categoryId).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString()+"---"+response.body().toString());
                mView.setDelRes(res(response),pos,sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
            }
        });
    }

    @Override
    public void updateMaterialCategory(int id,Integer level, String name, Integer org, final AlertDialog viewDialog) {
        emanuService.updateMaterialCategory(id,level, name, org).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.setAddRes(res(response),viewDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }


}
